package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class ex2 extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField not;
	private JTextField search;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JLabel lblNewLabel_2;
	private JTextArea result;

	String[] word = new String[100]; // 금지어가 저장되는 배열
	int i = 0;
	int count =0; // 저장된 금지어의 갯수

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ex2 frame = new ex2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ex2() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 399, 312);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getNot());
		contentPane.add(getSearch());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getScrollPane());
		contentPane.add(getLblNewLabel_2());
	}
	
	public void search(){
		boolean flag = false;  // 금지어가 포함되어 있지 않은 경우
		int cnt = 0;  // 포함된 금지어의 갯수
		String w = "";
		String str = search.getText();
		if(str.equals("")) return;
		for(int i=0;i<count;i++){
			if(str.contains(word[i])){
				flag = true;
				cnt++;
				w +=word[i] +" ";
			}
		}
		if(flag){  // 금지어가 한개 이상 포함된 경우
			result.append("금지어가 포함되어 있습니다.\n");
			result.append("포함된 금지어 수: "+cnt);
			result.append(",포함된 금지어:"+w);
		}else{
			result.append("금지어가 포함되어 있지 않습니다.\n");
		}
		result.setCaretPosition(result.getText().length());
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("금지어");
			lblNewLabel.setBounds(12, 20, 57, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("검색어");
			lblNewLabel_1.setBounds(12, 63, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JTextField getNot() {
		if (not == null) {
			not = new JTextField();
			not.setBounds(81, 17, 181, 21);
			not.setColumns(10);
		}
		return not;
	}

	private JTextField getSearch() {
		if (search == null) {
			search = new JTextField();
			search.setBounds(81, 60, 181, 21);
			search.setColumns(10);
		}
		return search;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("추가");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {

//					while (i < word.length) {
//						if (!not.getText().equals("")) {
//							word[count] = not.getText();
//							count++;
//							
//							not.setText("");
//							not.requestFocus();
//							break;
//							
//						}
//					}
					
					
					
					if(not.getText().equals("")){ // 빈 공백이 아닐경우에
						return;
					}
						String[] temp = not.getText().split(" ");
						for(int i=0; i<temp.length;i++){
							word[count] = temp[i];
							count++;
							if(count>99){
								for(int j=0; j<99; j++){
									word[j] = word[j+1];
								}
								count = 99;
						}
					}
						result.append("금지어가 추가되었습니다.\n");
						result.append("추가된 금지어:"+Arrays.toString(temp)+"\n");
						result.setCaretPosition(result.getText().length());
				}
			});
			btnNewButton.setBounds(274, 16, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					try {
//						boolean flag = true; // 모든 값을 입력했을 때
//						for (int i = 0; i < count; i++) {
//							if (search.getText().contains(word[i])) {
//								flag = false;
//								result.append("\n금지어가 포함되어 있습니다.");
//								
//								search.setText("");
//								search.requestFocus();
//								break;
//								
//							}
//						}
//
//						if (flag)
//							result.append("\n 검색 되었습니다.");
//					} catch (Exception e) {
//						result.append("\n 더 추가할수없어요");
//				}	
					search();
						
				
					
				}
			});
			btnNewButton_1.setBounds(274, 59, 97, 23);
		}
		return btnNewButton_1;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 135, 359, 119);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("검색결과");
			lblNewLabel_2.setBounds(12, 110, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
